package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.*;
import tech.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Verify")
public class HouseVerifyController {
    @Autowired
    private IHouseService houseService;

    @Autowired
    private ILandlordService landlordService;

    @Autowired
    private IAgentService agentService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IES ies;

    @GetMapping("/IssueHouse/{agentId}")
    public String select(HttpServletRequest request, @PathVariable("agentId") Integer agentId) {
//        String agentId = request.getParameter("agentId");
        Agent agent = agentService.selectById(Integer.valueOf(agentId));
        request.setAttribute("agent", agent);
        List<House> houseList = houseService.selectByagentIds(Integer.valueOf(agentId));
        request.setAttribute("houseList", houseList);
        List<Landlord> landlordList = new ArrayList<>();
        for (int i = 0; i <houseList.size() ; i++) {
            Landlord landlord = landlordService.selectById(houseList.get(i).getLandlordId());
            landlordList.add(landlord);
        }
        request.setAttribute("landlordList", landlordList);

        List<Message> messageStates = messageService.selectByState(agent.getRegisterId());
        request.setAttribute("messageSize",messageStates.size());

        return "/personal_homepage/broker/appointmentTime";
    }


    @PostMapping("/Sure")
    public @ResponseBody Result updateState(Integer id,String housename,Integer landlordId) {
        int count = houseService.updateStateById(id);
        if (count == 1){
            Integer registerId = landlordService.selectById(landlordId).getRegisterId();
            Message message = new Message(registerId,"您的房源“"+housename+"”已被发布,您可以在我的房源一栏进行查看.",new Date());
            messageService.insertAll(message);
            ies.addEsDoc();
            ies.addEsDocZheng();
            return new Result("200","确认发布");
        }else{
            return new Result("500","确认失败");
        }
    }



    @PostMapping("/Cancel")
    public @ResponseBody Result delById(Integer id,String housename,Integer landlordId,Integer agentId) {
        int count = houseService.deleteById(id);
        if (count == 1){
            Agent agent = agentService.selectById(agentId);
            Integer registerId = landlordService.selectById(landlordId).getRegisterId();
            Message message = new Message(registerId,"您的房源“"+housename+"”未被审核通过,请重新发布,具体原因请咨询经纪人:"+agent.getName()+",电话:"+agent.getPhone()+".",new Date());
            messageService.insertAll(message);
            return new Result("200","确认发布");
        }else{
            return new Result("500","确认失败");
        }
    }
}
